package com.uhu;

import com.uhu.code.*;
import com.uhu.code.help.HelpUtils;
import com.uhu.code.help.ListNode;
import org.junit.Test;

public class CodeApplication {

    /**
     * 1. 两数之和
     * https://leetcode.cn/problems/two-sum/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code1() {
        Solution1 sl = new Solution1();
        HelpUtils.outObj(sl.twoSum(new int[]{2, 7, 11, 15}, 9));
    }

    /**
     * 2. 两数相加
     * https://leetcode.cn/problems/add-two-numbers/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code2() {
        Solution2 sl = new Solution2();
        HelpUtils.outObj(sl.addTwoNumbers(HelpUtils.initListNode(new int[]{2, 4, 3}), HelpUtils.initListNode(new int[]{5, 6, 4})));
    }

    /**
     * 3. 无重复字符的最长子串
     * https://leetcode.cn/problems/longest-substring-without-repeating-characters/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code3() {
        Solution3 sl = new Solution3();
        HelpUtils.outObj(sl.lengthOfLongestSubstring("abcabcbb"));
    }

    /**
     * 11. 盛最多水的容器
     * https://leetcode.cn/problems/container-with-most-water/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code11() {
        Solution11 sl = new Solution11();
        HelpUtils.outObj(sl.maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}));
    }

    /**
     * 15. 三数之和
     * https://leetcode.cn/problems/3sum/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code15() {
        Solution15 sl = new Solution15();
        HelpUtils.outObj(sl.threeSum(new int[]{-1, 0, 1, 2, -1, -4}));
    }

    /**
     * 19. 删除链表的倒数第 N 个结点
     * https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code19() {
        Solution19 sl = new Solution19();
        HelpUtils.outObj(sl.removeNthFromEnd(HelpUtils.initListNode(new int[]{1, 2, 3, 4, 5}), 2));
    }

    /**
     * 21. 合并两个有序链表
     * https://leetcode.cn/problems/merge-two-sorted-lists/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code21() {
        Solution21 sl = new Solution21();
        HelpUtils.outObj(sl.mergeTwoLists(HelpUtils.initListNode(new int[]{1, 2, 4}), HelpUtils.initListNode(new int[]{1, 3, 4})));
    }

    /**
     * 24. 两两交换链表中的节点
     * https://leetcode.cn/problems/swap-nodes-in-pairs/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code24() {
        Solution24 sl = new Solution24();
        HelpUtils.outObj(sl.swapPairs(HelpUtils.initListNode(new int[]{1, 2, 3})));
        HelpUtils.outObj(sl.swapPairs(HelpUtils.initListNode(new int[]{1, 2, 3, 4})));
    }

    /**
     * 25. K 个一组翻转链表
     * https://leetcode.cn/problems/reverse-nodes-in-k-group/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code25() {
        Solution25 sl = new Solution25();
        HelpUtils.outObj(sl.reverseKGroup(HelpUtils.initListNode(new int[]{1, 2, 3, 4, 5}), 2));
        HelpUtils.outObj(sl.reverseKGroup(HelpUtils.initListNode(new int[]{1, 2, 3, 4, 5}), 3));
    }

    /**
     * 41. 缺失的第一个正数
     * https://leetcode.cn/problems/first-missing-positive/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code41() {
        Solution41 sl = new Solution41();
        HelpUtils.outObj(sl.firstMissingPositive(new int[]{1, 2, 0}));
        HelpUtils.outObj(sl.firstMissingPositive(new int[]{3, 4, -1, 1}));
        HelpUtils.outObj(sl.firstMissingPositive(new int[]{7, 8, 9, 11, 12}));
        HelpUtils.outObj(sl.firstMissingPositive(new int[]{1}));
    }

    /**
     * 48. 旋转图像
     * https://leetcode.cn/problems/rotate-image/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code48() {
        Solution48 sl = new Solution48();
        int nums[][] = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        sl.rotate(nums);
        HelpUtils.outObj(nums);
    }

    /**
     * 49. 字母异位词分组
     * https://leetcode.cn/problems/group-anagrams/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code49() {
        Solution49 sl = new Solution49();
        HelpUtils.outObj(sl.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
    }

    /**
     * 53. 最大子数组和
     * https://leetcode.cn/problems/maximum-subarray/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code53() {
        Solution53 sl = new Solution53();
        HelpUtils.outObj(sl.maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
    }

    /**
     * 54. 螺旋矩阵
     * https://leetcode.cn/problems/spiral-matrix/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code54() {
        Solution54 sl = new Solution54();
        HelpUtils.outObj(sl.spiralOrder(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}));
        HelpUtils.outObj(sl.spiralOrder(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}));
    }

    /**
     * 56. 合并区间
     * https://leetcode.cn/problems/merge-intervals/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code56() {
        Solution56 sl = new Solution56();
        HelpUtils.outObj(sl.merge(new int[][]{{1, 3}, {2, 6}, {8, 10}, {15, 18}}));
        HelpUtils.outObj(sl.merge(new int[][]{{1, 3}}));
    }

    /**
     * 73. 矩阵置零
     * https://leetcode.cn/problems/set-matrix-zeroes/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code73() {
        Solution73 sl = new Solution73();
        int[][] o1 = new int[][]{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
        sl.setZeroes(o1);
        HelpUtils.outObj(o1);
    }

    /**
     * 76.最小覆盖子串
     * https://leetcode.cn/problems/minimum-window-substring/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code76() {
        Solution76 sl = new Solution76();
        HelpUtils.outObj(sl.minWindow("ADOBECODEBANC", "ABC"));
        HelpUtils.outObj(sl.minWindow("a", "a"));
        HelpUtils.outObj(sl.minWindow("a", "aa"));
        HelpUtils.outObj(sl.minWindow("a", "b"));
        HelpUtils.outObj(sl.minWindow("aa", "aa"));
    }

    /**
     * 128. 最长连续序列
     * https://leetcode.cn/problems/longest-consecutive-sequence/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code128() {
        Solution128 sl = new Solution128();
        HelpUtils.outObj(sl.longestConsecutive(new int[]{100, 4, 200, 1, 3, 2}));
    }

    /**
     * 141.环形链表
     * https://leetcode.cn/problems/linked-list-cycle/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code141() {
        Solution141 sl = new Solution141();
        ListNode head = HelpUtils.initListNode(new int[]{3});
        ListNode listNodeB = HelpUtils.initListNode(new int[]{2, 0, -4});
        head.setNext(listNodeB);
        listNodeB.getNext().getNext().setNext(listNodeB);
        HelpUtils.outObj(sl.hasCycle(head));
    }

    /**
     * 142.环形链表II
     * https://leetcode.cn/problems/linked-list-cycle-ii/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code142() {
        Solution142 sl = new Solution142();
        ListNode head = HelpUtils.initListNode(new int[]{3});
        ListNode listNodeB = HelpUtils.initListNode(new int[]{2, 0, -4});
        head.setNext(listNodeB);
        listNodeB.getNext().getNext().setNext(listNodeB);
        HelpUtils.outObj(sl.detectCycle(head));
    }

    /**
     * 160.相交链表
     * https://leetcode.cn/problems/intersection-of-two-linked-lists/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code160() {
        Solution160 sl = new Solution160();
        ListNode listNodeA = HelpUtils.initListNode(new int[]{4, 1});
        ListNode listNodeB = HelpUtils.initListNode(new int[]{5, 6, 1});
        ListNode listNodeC = HelpUtils.initListNode(new int[]{8, 4, 5});
        listNodeA.getNext().setNext(listNodeC);
        listNodeB.getNext().getNext().setNext(listNodeC);
        HelpUtils.outObj(sl.getIntersectionNode(listNodeA, listNodeB));
    }

    /**
     * 189.轮转数组
     * https://leetcode.cn/problems/rotate-array/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code189() {
        Solution189 sl = new Solution189();
        int nums[] = new int[]{1, 2, 3, 4, 5, 6, 7};
        sl.rotate(nums, 3);
        HelpUtils.outObj(nums);
        int nums1[] = new int[]{1};
        sl.rotate(nums1, 0);
        HelpUtils.outObj(nums1);
    }

    /**
     * 206.反转链表
     * https://leetcode.cn/problems/reverse-linked-list/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code206() {
        Solution206 sl = new Solution206();
        ListNode listNodeA = HelpUtils.initListNode(new int[]{1, 2, 3, 4, 5});
        HelpUtils.outObj(sl.reverseList(listNodeA));
    }

    /**
     * 234.回文链表
     * https://leetcode.cn/problems/palindrome-linked-list/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code234() {
        Solution234 sl = new Solution234();
        HelpUtils.outObj(sl.isPalindrome(HelpUtils.initListNode(new int[]{1, 2, 2, 1})));
        HelpUtils.outObj(sl.isPalindrome(HelpUtils.initListNode(new int[]{1, 0, 1})));
    }

    /**
     * 238. 除自身以外数组的乘积
     * https://leetcode.cn/problems/product-of-array-except-self/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code238() {
        Solution238 sl = new Solution238();
        HelpUtils.outObj(sl.productExceptSelf(new int[]{1, 2, 3, 4}));
        HelpUtils.outObj(sl.productExceptSelf(new int[]{-1, 1, 0, -3, 3}));
    }

    /**
     * 239.滑动窗口最大值
     * https://leetcode.cn/problems/sliding-window-maximum/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code239() {
        Solution239 sl = new Solution239();
        HelpUtils.outObj(sl.maxSlidingWindow(new int[]{1, 3, -1, -3, 5, 3, 6, 7}, 3));
        HelpUtils.outObj(sl.maxSlidingWindow(new int[]{1}, 1));
    }

    /**
     * 240. 搜索二维矩阵 II
     * https://leetcode.cn/problems/search-a-2d-matrix-ii/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code240() {
        Solution240 sl = new Solution240();
        HelpUtils.outObj(sl.searchMatrix(new int[][]{{1, 4}, {2, 5}}, 5));
    }

    /**
     * 283. 移动零
     * https://leetcode.cn/problems/move-zeroes/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code283() {
        Solution283 sl = new Solution283();
        int obj[] = new int[]{0, 1, 0, 3, 12};
        sl.moveZeroes(obj);
        HelpUtils.outObj(obj);
    }

    /**
     * 438. 找到字符串中所有字母异位词
     * https://leetcode.cn/problems/find-all-anagrams-in-a-string/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code438() {
        Solution438 sl = new Solution438();
        HelpUtils.outObj(sl.findAnagrams("cbaebabacd", "abc"));
    }

    /**
     * 560. 和为K的子数组
     * https://leetcode.cn/problems/subarray-sum-equals-k/description/?envType=study-plan-v2&envId=top-100-liked
     */
    @Test
    public void code560() {
        Solution560 sl = new Solution560();
        HelpUtils.outObj(sl.subarraySum(new int[]{1, 1, 1}, 2));
    }
}
